﻿using LBoL.Base;
using Newtonsoft.Json;
using SimpleJSON;
using System;
using System.Collections.Generic;

namespace MBP.ModBase
{
    public class ModCard
    {
        #region BaseData
        public string id { get; set; }
        public string ownner { get; set; }
        public CardType card_type { get; set; }
        public TargetType target_type { get; set; }
        public Rarity rarity { get; set; }
        public List<ManaColor> color { get; set; }
        public List<string> keywords { get; set; }
        public List<string> plus_keywords { get; set; }
        /// <summary>
        /// 关联卡牌
        /// 被关联的卡牌将会出现在悬浮窗上
        /// </summary>
        public List<string> relative_cards { get; set; }
        /// <summary>
        /// 是否出现在随机池
        /// </summary>
        public bool is_pooled { get; set; }
        /// <summary>
        /// 是否能敲
        /// </summary>
        public bool is_upgradable { get; set; }
        #endregion
        /// <summary>
        /// 卡牌值词典
        /// </summary>
        public Dictionary<string, int> card_datas_dic { get; set; }
        /// <summary>
        /// 升级后卡牌值词典
        /// </summary>
        public Dictionary<string, int> card_plus_datas_dic { get; set; }

        #region Img
        public Dictionary<string, string> img_dic { get; set; }
        #endregion

        [JsonIgnore]
        public string modid { get; set; }

        public override string ToString()
        {
            return $"[~ModCard:ID:{id}]";
        }

        public int GetDataValue(string key,bool isPlus)
        {
            bool hasValue;
            int o;
            if (!isPlus)
            {
                hasValue = card_datas_dic.TryGetValue(key, out o);
            }
            else
            {
                hasValue = card_plus_datas_dic.TryGetValue(key, out o);
            }
            return hasValue ? o : 0;
        }
        
        public Keyword GetKeyword(bool isPlus)
        {
            Keyword o = Keyword.None;
            List<string> kList;
            if (!isPlus)
            {
                kList = keywords;
            }
            else
            {
                kList = plus_keywords;
            }

            foreach(var k in kList)
            {
                var ke = (Keyword)Enum.Parse(typeof(Keyword), k);
                o = o | ke;
            }

            return o;
        }

        public ModCard(JSONNode jsonNode)
        {
            id = jsonNode["id"];
            ownner = jsonNode["ownner"];
            card_type = (CardType)jsonNode["card_type"].AsInt;
            target_type = (TargetType)jsonNode["target_type"].AsInt;
            rarity = (Rarity)jsonNode["rarity"].AsInt;
            color = stringListToColorList(jsonNode["color"].AsStringList);
            keywords = jsonNode["keywords"].AsStringList;
            plus_keywords = jsonNode["plus_keywords"].AsStringList;
            relative_cards = jsonNode["relative_cards"].AsStringList;
            is_pooled = jsonNode["is_pooled"].AsBool;
            is_upgradable = jsonNode["is_upgradable"].AsBool;
            //处理Dictionary反序列化
            var card_datas_dic_json = jsonNode["card_datas_dic"];
            card_datas_dic = new Dictionary<string, int>();
            foreach(JSONNode j in card_datas_dic_json.Children)
            {
                card_datas_dic.Add(j[0], j[1].AsInt);
            }

            var card_plus_datas_dic_json = jsonNode["card_plus_datas_dic"];
            card_plus_datas_dic = new Dictionary<string, int>();
            foreach (JSONNode j in card_plus_datas_dic_json.Children)
            {
                card_plus_datas_dic.Add(j[0], j[1].AsInt);
            }

            var img_dic_json = jsonNode["img_dic"];
            img_dic = new Dictionary<string, string>();
            foreach (JSONNode j in img_dic_json.Children)
            {
                img_dic.Add(j[0], j[1]);
            }
        }

        private static List<ManaColor> stringListToColorList(List<string> slist)
        {
            var iList = new List<ManaColor>();
            slist.ForEach(i =>
            {
                iList.Add((ManaColor)int.Parse(i));
            });
            return iList;
        }
    }
}
